#!/bin/bash
###
 # @Author: dvlproad
 # @Date: 2023-02-28 14:15:04
 # @LastEditors: dvlproad
 # @LastEditTime: 2023-06-07 19:36:24
 # @Description: 
### 
:<<!
检查本分支是否可以打包(①是否是允许的分支;②本次打包是否漏掉本分支上次打包的分支)
!
#sh all_packing_allow.sh -envJsonF "${ENVIRONMENT_FILE_PATH}"
JQ_EXEC=`which jq`

# shell 参数具名化
show_usage="args: [-envJsonF]\
                                  [--environment-json-file=]"

while [ -n "$1" ]
do
        case "$1" in
                -envJsonF|--environment-json-file) ENVIRONMENT_FILE_PATH=$2; shift 2;;
                --) break ;;
                *) echo ""; break ;;
        esac
done

exit_script() { # 退出脚本的方法，省去当某个步骤失败后，还去继续多余的执行其他操作
    exit 1
}


Develop_Branchs_FILE_PATH=$(cat ${ENVIRONMENT_FILE_PATH} | jq .developBranchsJsonFile | sed 's/\"//g')
History_Versions_FILE_PATH=$(cat ${ENVIRONMENT_FILE_PATH} | ${JQ_EXEC} .historyVersionJsonFile | sed 's/\"//g')
Product_Personnel_FILE_PATH=$(cat ${ENVIRONMENT_FILE_PATH} | ${JQ_EXEC} .productPersonnelJsonFile | sed 's/\"//g')

# 是数组时候
# BRANCH=$(cat $ENVIRONMENT_FILE_PATH | ${JQ_EXEC} .package_from_brance | sed 's/\"//g')
# PlatformType=$(cat $ENVIRONMENT_FILE_PATH | ${JQ_EXEC} .platform_type | sed 's/\"//g')
# PackageTargetType=$(cat $ENVIRONMENT_FILE_PATH | ${JQ_EXEC} .package_default_target | sed 's/\"//g')

# PackageNetworkType=$(cat ${ENVIRONMENT_FILE_PATH} | ${JQ_EXEC} .package_default_env)
# network_PgyerRootMapString=$(cat ${ENVIRONMENT_FILE_PATH} | ${JQ_EXEC} -r ".package_pgyer_params[] | select(.network==\"${PackageNetworkType}\")")

# 当前【shell脚本】的工作目录
# $PWD代表获取当前路径，当cd后，$PWD也会跟着更新到新的cd路径。这个和在终端操作是一样的道理的
CurrentDIR_Script_Absolute="$( cd "$( dirname "$0" )" && pwd )"
#echo "CurrentDIR_Script_Absolute=${CurrentDIR_Script_Absolute}"

PRODUCT_WORKSPACE=${CurrentDIR_Script_Absolute%/*}
echo "✅项目PRODUCT_WORKSPACE=${PRODUCT_WORKSPACE}"
# cd ${PRODUCT_WORKSPACE} # 📢不需要使用git命令，所以这里不需要跳转
# 是否是使用从git上拉取的公共脚本项目(在其他项目中会这样引入本git过程)
function commonScriptProductIsFromGitclone() {
    if [ "$1" == "true" ]; then
        BUILD_SCRIPT_PRODUCT_DIR_NAME="buildScriptSource"
        BUILD_SCRIPT_PRODUCT_DIR_PATH="${PRODUCT_WORKSPACE}/${BUILD_SCRIPT_PRODUCT_DIR_NAME}"
        # if [ ! -d "${BUILD_SCRIPT_PRODUCT_DIR_PATH}" ]; then
        #     git clone -b 0.2.0 https://gitee.com/dvlpCI/AutoPackage-CommitInfo "${BUILD_SCRIPT_PRODUCT_DIR_NAME}"
        # else
        #     cd "${BUILD_SCRIPT_PRODUCT_DIR_PATH}"
        #     git reset --hard 0.2.0
        # fi
    else
        BUILD_SCRIPT_PRODUCT_DIR_PATH="${PRODUCT_WORKSPACE}"
    fi
}
commonScriptProductIsFromGitclone "true"
bulidScriptCommon_dir_Absolute="${BUILD_SCRIPT_PRODUCT_DIR_PATH}/bulidScriptCommon"
echo "bulidScriptCommon_dir_Absolute=${bulidScriptCommon_dir_Absolute}"


#检查当前分支是否可以打包
echo "\n\n\n"
echo "检查当前分支是否可以打包"
sh ${bulidScriptCommon_dir_Absolute}/branchs_can_pack/check_branch_can_pack_env.sh "${ENVIRONMENT_FILE_PATH}"          # 检查本分支是否可以打包指定环境
if [ $? != 0 ]; then
    exit_script
fi

currentBranch=$(git branch --show-current) # 获取当前分支
currentBranchStringLength=${#currentBranch}
if [ ${currentBranchStringLength} == 0 ]; then
    echo "提示：此电脑环境未获取到本地分支(可能是jenkins问题),故将直接取直接传进来的分支"
    currentBranch=$(cat ${ENVIRONMENT_FILE_PATH} | ${JQ_EXEC} '.package_from_brance' | sed 's/\"//g')
fi
currentBranch=${currentBranch##*/} # 取最后的component
echo "currentBranch=${currentBranch}"

if [ ${currentBranch} == "dev_publish_in" ]; then
    # mustContainBranchNameArray=("dev_publish_out")
    mustContainBranchNameArray=()
elif [ ${currentBranch} == "dev_all" ]; then
    # mustContainBranchNameArray=("dev_in_pgyer")
    mustContainBranchNameArray=()
    ExtraMessageIfError="[附：测试包目前必须包含蒲公英dev_in_pgyer功能.\
    (虽然内测功能dev_publish_in分支已包含dev_in_pgyer,但为避免有新commit提交到dev_publish_in时,导致dev_all得频繁rebase变基。\
    所以dev_all改为rebase自development,并merger进dev_in_pgyer)]"
fi
echo "正在执行命令(检查分支是否包含应该包含的分支):《 sh ${bulidScriptCommon_dir_Absolute}/branchs_can_pack/check_branch_must_contain.sh \"${Develop_Branchs_FILE_PATH}\" \"${currentBranch}\" \"${mustContainBranchNameArray[*]}\" \"${ExtraMessageIfError}\" 》"
sh ${bulidScriptCommon_dir_Absolute}/branchs_can_pack/check_branch_must_contain.sh "${Develop_Branchs_FILE_PATH}" "${currentBranch}" "${mustContainBranchNameArray[*]}" "${ExtraMessageIfError}" # 检查本分支有没有合入必须合入的分支
if [ $? != 0 ]; then
    exit_script
fi


echo "\n------检查本分支上个包和和本分支此次包的需求包含------"
# 增加本分支上个包和和本分支此次包的需求包含检查
#FileName="${VERSION}(${BUILD}).json"
FileName="${currentBranch}.json"


# lastPackageInfoFile_dir_Absolute="../.."
#lastPackageInfoFile_dir_Absolute=${${CurrentDIR_Script_Absolute%/*}%/*}  # 使用此方法可以避免路径上有../../ 但是无法写在sh文件中，虽然此命令在终端上运行是成功的。所以得一层一层处理
workspace_dir_Absolute=${CurrentDIR_Script_Absolute%/*}
lastPackageInfoFile_dir_Absolute=${workspace_dir_Absolute%/*}
lastPackageInfoFilePath="${lastPackageInfoFile_dir_Absolute}/${FileName}"
#echo ">>>>>>>>>>>>上次打包的分支信息文件lastPackageInfoFilePath=${lastPackageInfoFilePath},CurrentDIR_Script_Absolute=${CurrentDIR_Script_Absolute}"
# 检查本分支上个包和和本分支此次包的需求包含
echo "正在执行命令(检查本分支的本次打包是否漏掉本分支上次打包的分支)：《sh ${bulidScriptCommon_dir_Absolute}/branchs_can_pack/check_branch_diff_old.sh -envJsonF \"${ENVIRONMENT_FILE_PATH}\" --last-package-json-file \"${lastPackageInfoFilePath}\" --develop-branchs-json-file \"${Develop_Branchs_FILE_PATH}\" --history-version-json-file \"${History_Versions_FILE_PATH}\" --product-personnel-json-file \"${Product_Personnel_FILE_PATH}\"》"
sh ${bulidScriptCommon_dir_Absolute}/branchs_can_pack/check_branch_diff_old.sh -envJsonF "${ENVIRONMENT_FILE_PATH}" --last-package-json-file "${lastPackageInfoFilePath}" --develop-branchs-json-file "${Develop_Branchs_FILE_PATH}" --history-version-json-file "${History_Versions_FILE_PATH}" --product-personnel-json-file "${Product_Personnel_FILE_PATH}"
if [ $? != 0 ]; then
    exit 1
fi
echo "当前分支可以打包，继续执行"









